package com.wjjun.test.ui

import android.view.View
import android.webkit.WebView
import android.widget.Toast
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.lifecycleScope
import com.wjjun.base.ui.activity.BaseListActivity
import com.wjjun.base.ui.viewmodel.BaseItemViewModel
import com.wjjun.base.ui.viewmodel.BaseViewModel
import com.wjjun.data.respository.TestRepository
import com.wjjun.library.R
import com.wjjun.test.ui.itemviewmodel.ArticleItemViewModel
import com.wjjun.test.ui.viewmodel.TestReqViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import javax.inject.Inject
import kotlin.contracts.ExperimentalContracts
import kotlin.coroutines.suspendCoroutine

/**
 * 测试网络请求
 * author : wangjunjun
 * date   : 2022/7/20
 * desc   :
 */
@AndroidEntryPoint
class TestReqActivity : BaseListActivity<TestReqViewModel, ViewDataBinding>() {

    @Inject
    lateinit var testRepository: TestRepository

    override fun initContent() {
        setTitle("测试请求列表界面")
        setRefreshEnable(true)
        setLoadMoreEnable(true)
    }

    override fun initParams() {
    }

    override suspend fun fetchData() {
        super.fetchData()
        val banner = arrayListOf<String>()

        testRepository.getBanner().forEach {
            banner += it.imagePath
        }
        viewModel.bannerList.value = banner
    }

    override suspend fun fetchData(pageNum: Int, pageSize: Int, useCache: Boolean, param: Any?) {
        val list = arrayListOf<BaseItemViewModel<*>>()
        val res = testRepository.getIndexList(pageNum)
        res.datas.forEach {
            list.add(ArticleItemViewModel(it))
        }
        setData(list, pageNum)
    }

    override fun onItemClick(view: View, itemViewModel: BaseViewModel) {
        if (itemViewModel is ArticleItemViewModel) {
            Toast.makeText(this, itemViewModel.getData()?.title, Toast.LENGTH_SHORT).show()
        }
    }

    override fun onClick(view: View) {

    }
}